﻿using Sustainalytics.Claims;
using Sustainalytics.ClientDW.Entities;
using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Sustainalytics.GCCS.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[AutoInvalidateCacheOutput]
    [RoutePrefix("api/v1/gcprincipleconclusion")]
    public class GCPrincipleConclusionController : ApiController
    {
         IGCPrincipleConclusionManager _manager;
         IClaimsChecker _claimsChecker;

        public GCPrincipleConclusionController(IGCPrincipleConclusionManager manager, IClaimsChecker claimsChecker)
        {
            _manager = manager;
            _claimsChecker = claimsChecker;
        }


       // [CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("")]
        public HttpResponseMessage Get(Guid principleSummaryId)
        {
            try
            {
                if (!_claimsChecker.ValidateNonCompliantOrWatchlist(this))
                {
                    return this.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }

                // get principle conclusion
                GCPrincipleConclusion ent = _manager.GetByPrincipleSummary(principleSummaryId);

                // project to DTO
                GCPrincipleConclusionDto dto = new GCPrincipleConclusionDto().LoadFrom(ent);

                return this.Request.CreateResponse(HttpStatusCode.OK, dto);
            }
            catch (GCPrincipleConclusionNotFoundException ex)
            {
                var dto = ex.GetHttpResponseMessageDto();
                return this.Request.CreateResponse(HttpStatusCode.NotFound, dto);
            }
        }
    }
}
